package com.cskaoyan.controller;

import com.cskaoyan.bean.bo.AdminCommentDeleteBO;
import com.cskaoyan.bean.bo.AdminCommentListBO;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.vo.AdminCommentListVO;
import com.cskaoyan.service.AdminCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author zhuqiushi
 * @since 2022/09/07 21:05
 */
@RequestMapping("admin/comment")
@RestController
public class AdminCommentController {
    @Autowired
    AdminCommentService adminCommentService;

    /*
     * /admin/comment/list?page=1&limit=20&userId=1&valueId=1&sort=add_time&order=desc
     * */
    @RequestMapping("list")
    public BaseRespVo<AdminCommentListVO> list(@Valid AdminCommentListBO adminCommentListBO,
                                               BindingResult bindingResult) {
        //调用service层查询
        AdminCommentListVO adminCommentListVO = adminCommentService.list(
                adminCommentListBO.getPage(), adminCommentListBO.getLimit(), adminCommentListBO.getValueId(),
                adminCommentListBO.getUserId(),
                adminCommentListBO.getSort(), adminCommentListBO.getOrder());
        //将结果封装返回
        BaseRespVo ok = BaseRespVo.ok(adminCommentListVO);
        return ok;
    }

    /*
     * /admin/comment/delete
     * post
     * */
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody @Valid AdminCommentDeleteBO adminCommentDeleteBO,
                             BindingResult bindingResult) {
        //调用service删除
        int code = adminCommentService.delete(adminCommentDeleteBO);
        BaseRespVo<Object> respVo = new BaseRespVo<>();
        respVo.setErrno(code);
        if (code == 402) {
            respVo.setErrmsg("参数错误");
        } else if (code == 0) {
            respVo.setErrmsg("成功");
        }
        return respVo;
    }
}
